package com.imageim.yimirror.common.ui;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imageim.yimirror.R;
import com.imageim.yimirror.common.util.LogUtil;
import com.imageim.yimirror.common.util.TimeUtils;
import com.imageim.yimirror.service.weather.bean.WeatherDetail;
import com.imageim.yimirror.service.weather.bean.WeatherItem;

/**
 * Created by Bruce.Zhou on 2018/6/7 16:51.
 * Email: 907160968@qq.com
 */

public class WeatherView extends RelativeLayout {
    public WeatherView(Context context) {
        super(context);
        initView();
    }

    public WeatherView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public WeatherView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private ImageView ivWeatherIcon;
    private TextView tvWeatherDesc;
    private TextView tvTemperature;
    private TextView tvWind;
    private TextView tvTime;
    private TextView tvChuanyi;
    private TextView tvOutsideAdvise;

    private void initView() {
        View view = View.inflate(getContext(), R.layout.layout_weather, this);
        ivWeatherIcon = view.findViewById(R.id.iv_weather_icon);
        tvTemperature = view.findViewById(R.id.tv_temperature);
        tvWeatherDesc = view.findViewById(R.id.tv_weather_desc);
        tvWind = view.findViewById(R.id.tv_wind);
        tvTime = view.findViewById(R.id.tv_time);
        tvChuanyi = view.findViewById(R.id.tv_chuanyi);
        tvOutsideAdvise = view.findViewById(R.id.tv_goout);
    }

    public void setWeather(WeatherDetail weather, String city) {
        int timeFlag = TimeUtils.getDawnDayNight();
        WeatherItem item = weather.getWeather().get(0);
        //        tvWeatherDesc.setText("白天" + item.getInfoDay() + " 夜间" + item.getInfoNight());
        tvWeatherDesc.setText(city + "   " + weather.getInfo());
        tvTemperature.setText(item.getTemperatureNight() + "~" + item.getTemperatureDay() + "℃");
        if (timeFlag == 2) {
            tvWind.setText(item.getWindDay());
        } else if (timeFlag == 3) {
            tvWind.setText(item.getWindNight());
        } else {
            tvWind.setText(item.getWindDawn());
        }
        if (weather.getWeek() >= weeks.length) {
            return;
        }else {
            tvTime.setText(weather.getDate() + "   " + weeks[weather.getWeek()] + "        农历" + weather.getWeather().get(0).getNongli());
        }
        setWeatherIcon(weather.getImg());
        LogUtil.d("weather", weather.getImg() + "");
        tvChuanyi.setText("穿衣：" + removeLastCh(weather.getChuanYi()));
        if (!TextUtils.isEmpty(item.getDayOutAdvise())) {
            tvOutsideAdvise.setText("外出：" + item.getDayOutAdvise());
        } else {
            tvOutsideAdvise.setVisibility(View.GONE);
        }
    }

    public void setWeatherIcon(int imgIndex) {
        switch (imgIndex) {
            case 0:
                ivWeatherIcon.setImageResource(R.drawable.ic_weather_sunny);
                break;
            case 1: // 多云
                ivWeatherIcon.setImageResource(R.drawable.ic_weather_sunny_cloud);
                break;
            case 2:  // 阴天
                ivWeatherIcon.setImageResource(R.drawable.ic_weather_more_cloud);
                break;
            case 3: // 阵雨
                ivWeatherIcon.setImageResource(R.drawable.ic_weather_vbig_rain);
                break;
            case 4: // 雷阵雨
                ivWeatherIcon.setImageResource(R.drawable.ic_weather_thunder_rain);
                break;
            case 7: // 小雨
                ivWeatherIcon.setImageResource(R.drawable.ic_weather_little_rain);
                break;
            case 8: // 中雨
                ivWeatherIcon.setImageResource(R.drawable.ic_weather_middle_rain);
                break;
            case 9: // 大雨
                ivWeatherIcon.setImageResource(R.drawable.ic_weather_big_rain);
                break;
            case 10: // 暴雨
                ivWeatherIcon.setImageResource(R.drawable.ic_weather_big_rain);
                break;
            default:
                ivWeatherIcon.setImageResource(R.drawable.filter_res_hold);
                break;
        }
    }

    private String removeLastCh(String input) {
        if (input == null || input.length() == 0) {
            return input;
        }
        char lastCh = input.charAt(input.length() - 1);
        if (lastCh == '。') {
            input = input.substring(0, input.length() - 1);
        }
        return input;
    }

    private String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

}
